import { Component, OnInit } from '@angular/core';
import { ApplyFundGroupService } from './apply-fund-group.service';
import { DataService } from '../../../../../services/data.service';
import * as _ from "lodash";

@Component({
  selector: 'app-apply-fund-group',
  templateUrl: './apply-fund-group.component.html',
  styleUrls: ['./apply-fund-group.component.scss'],
  providers: [ApplyFundGroupService]
})
export class ApplyFundGroupComponent implements OnInit {
   //发送过来的数据下标
   public index: any;
   //从上一层获取到的默认勾选的数据id
   public checkedIdList: any = [];
   //判断查询按钮是否已点击
   public searchIsChecked: any = false;
   //根据类型分类的list
   public fundTypeList: any;
   public fundTypeInfo: any;
   //根据搜索条件得到的list
   public searchList: any = [];
   //基金组list
   public groupList: any = [];
   //账户名称
   public sFundName: any;
   //产品分类以及搜索全选按钮
   public typeCheckedAll: any = false;
   //基金组全选按钮
   public groupCheckedAll: any = false;

  constructor(
    public applyFundGroupService: ApplyFundGroupService,
    public dataService: DataService
  ) { }

  ngOnInit() {
    // console.log(this.checkedIdList)
    if(this.checkedIdList!=''){
      this.checkedIdList = this.checkedIdList.split(',');
        //将接受到的数据转为number类型
    _.forEach(this.checkedIdList, (item, index) => {
      this.checkedIdList[index] = parseInt(this.checkedIdList[index]);
    })
    }
    console.log('传过来的参数');
    console.log(typeof this.checkedIdList[0]);
    this.listByFundType();
    // this.fundGroupList();
  }

// 列表全选
checkedAll(data, checkedAll) {
  
      if (checkedAll) {
        // 更新为全选
  
        _.forEach(data, item => {
          item.isChecked = true;
        });
      } else {
        // 更新为不全选
        _.forEach(data, item => {
          item.isChecked = false;
        });
      }
    }
    // 列表是否全选
    ctrlCheckAll(data, checkAllBtn) {
      let that = this;
      let checkList = [];
      _.forEach(data, item => {
        if (item.isChecked == true) {
          checkList.push(item);
        }
      })
      if (checkList.length == data.length) {
        this.checkedAllType(checkAllBtn, true);
      } else {
        this.checkedAllType(checkAllBtn, false);
      }
    }
    //判断全选按钮类型
    checkedAllType(checkAllBtn, isTrue) {
      switch (checkAllBtn) {
        //产品类型全选按钮
        case 'typeCheckedAll':
          this.typeCheckedAll = isTrue;
          break;
        //基金组全选按钮
        case 'groupCheckedAll':
          this.groupCheckedAll = isTrue;
          break;
      }
    }
    //适用名称搜索弹出框产品类型tab
    listByFundType() {
      let that = this;
      let getBody = {
        sType: 1  //0代表非QDII，1代表QDII
      }
  
      this.applyFundGroupService.listByFundType(getBody).subscribe(data => {
        if (data) {
          this.fundTypeInfo = data;
          console.log(data);
          console.log(this.fundTypeList)
          if (this.checkedIdList.length > 0) {
            _.forIn(that.fundTypeInfo, (value, key) => {
              _.forEach(value, item => {
                if (_.includes(that.checkedIdList, item.nFundNo)) {

                  item.isChecked = true;
                }
              })
            })
          }
          // console.log(data);
          this.selectType('公募QDII');
        }
      })
    }
    //点击tab根据产品类型将数据添加到list中
    selectType(type) {
      let that = this;
      _.forIn(this.fundTypeInfo, (value, key) => {
        // console.log(key);
        if (type == key) {
          that.fundTypeList = value;
          that.ctrlCheckAll(that.fundTypeList, 'typeCheckedAll');
        }
      })
    }
    //获取基金组信息列表
    fundGroupList() {
      let that = this;
      let getBody = {
        sType: 0
      }
      this.applyFundGroupService.fundGroupList(getBody).subscribe(data => {
        if (data) {
          console.log(data);
          this.groupList = data;
          if (this.checkedIdList.length > 0) {
            _.forEach(this.groupList, item => {
              if (_.includes(that.checkedIdList, item.nFgId)) {
                item.isChecked = true;
              }
            })
          }
        }
      })
    }
    //适用名称搜索弹出框搜索
    searchlist(item?) {
      let that = this;
      let getBody = {
        sFundName: this.sFundName,
        sType: 1
      }
      if (item == 'search') {
        this.searchIsChecked = true;
      }
      this.applyFundGroupService.searchList(getBody).subscribe(data => {
        if (data) {
          console.log(data);
          this.searchList = data;
          that.ctrlCheckAll(this.searchList, 'typeCheckedAll');
        }
      })
    }
    // //保存选择的用户
    // saveSelect() {
    //   let that = this;
    //   let saveList: any = [];
    //   let saveListItem: any;
    //   let sFundNoList: any = [];
    //   let sFundNoItem: any;
    //   _.forIn(this.fundTypeInfo, (value, key) => {
    //     _.forEach(value, item => {
    //       if (item.isChecked) {
    //         saveList.push(item.sFundName);
    //         sFundNoList.push(item.nFundNo);
    //       }
    //     })
    //   })
    //   _.forEach(this.searchList, item => {
    //     if (item.isChecked) {
    //       saveList.push(item.sFundName);
    //       sFundNoList.push(item.nFundNo);
    //     }
    //   })
    //   _.forEach(this.groupList, item => {
    //     if (item.isChecked) {
    //       saveList.push(item.sFgName);
    //       sFundNoList.push(item.nFgId);
    //     }
    //   })
    //   saveList = _.uniq(saveList);
    //   saveListItem = saveList.join(',');
    //   that.dataService.nQDIIFGroupInfo = saveListItem;
    //   sFundNoList = _.uniq(sFundNoList);
    //   sFundNoItem = sFundNoList.join(',');
 
    //   console.log(saveListItem)
    //   console.log(sFundNoItem)
    // }


//保存选择的用户
saveSelect() {
  let that = this;
  let saveList: any = [];
  let nFundNoList: any = [];
  let saveListItem: any;
  let nFundNos: any;
  //获取已勾选群组成员数据
  _.forIn(this.fundTypeInfo, (value, key) => {
    _.forEach(value, item => {
      if (item.isChecked) {
        if (item.sFundCode != null) {
          saveList.push(item.sFundCode);
        } else {
          saveList.push(item.nFundNo);
        }
      }
    })
  })
  _.forEach(this.searchList, item => {
    if (item.isChecked) {
      if (item.sFundCode != null) {
        saveList.push(item.sFundCode);
      } else {
        saveList.push(item.nFundNo);
      }
    }
  })
  //已勾选数据去重
  saveList = _.uniq(saveList);
  //已勾选数据转成字符串
  saveListItem = saveList.join(',');

  //获取已勾选数据nFundNo
  _.forIn(this.fundTypeInfo, (value, key) => {
    _.forEach(value, item => {
      if (item.isChecked) {
        nFundNoList.push(item.nFundNo);
      }
    })
  })
  _.forEach(this.searchList, item => {
    if (item.isChecked) {
      nFundNoList.push(item.nFundNo);
    }
  })
  nFundNoList = _.uniq(nFundNoList);
  nFundNos = nFundNoList.join(',');

  if(nFundNoList.length==0){
    that.dataService.nQDIIsFgMemberValidEmitter.emit('no data');
    that.dataService.nQDIIsMemberFundnosEmitter.emit('no data');
  }else{
    that.dataService.nQDIIsFgMemberValidEmitter.emit(saveListItem);
    that.dataService.nQDIIsMemberFundnosEmitter.emit(nFundNos);
  }

  console.log(saveListItem);
  console.log(nFundNos);
}


  // //保存选择的用户
  // saveSelect() {
  //   let that = this;
  //   let saveList: any = [];
  //   let saveListItem: any;
  //   let nFundNoList: any = [];
  //   let nFundNos: any;
  //   //获取已勾选群组成员数据
  //   _.forIn(this.fundTypeInfo, (value, key) => {
  //     _.forEach(value, item => {
  //       if (item.isTypeChecked) {
  //         if (item.sFundCode != null) {
  //           saveList.push(item.sFundCode);
  //         } else {
  //           saveList.push(item.nFundNo);
  //         }
  //       }
  //     })
  //   })
  //   _.forEach(this.searchList, item => {
  //     if (item.isSearchChecked) {
  //       if (item.sFundCode != null) {
  //         saveList.push(item.sFundCode);
  //       } else {
  //         saveList.push(item.nFundNo);
  //       }
  //     }
  //   })
  //   saveList = _.uniq(saveList);
  //   saveListItem = saveList.join(',');

  //   //获取已勾选数据nFundNo
  //   _.forIn(this.fundTypeInfo, (value, key) => {
  //     _.forEach(value, item => {
  //       if (item.isTypeChecked) {
  //         nFundNoList.push(item.nFundNo);
  //       }
  //     })
  //   })
  //   _.forEach(this.searchList, item => {
  //     if (item.isSearchChecked) {
  //       nFundNoList.push(item.nFundNo);
  //     }
  //   })
  //   nFundNoList = _.uniq(nFundNoList);
  //   nFundNos = nFundNoList.join(',');

  //   that.dataService.nQDIIsFgMemberValidEmitter.emit(saveListItem);
  //   that.dataService.nQDIIsMemberFundnosEmitter.emit(nFundNos);
  //   console.log(saveListItem);
  //   console.log(nFundNos);
  // }



  hideModal() {
    window['$']('#applyFundGroup').modal('hide');
  }

}
